
const teacher = {
    state: {    //存储的字段
        teacherId: '',  //用户id
        teacherName: '',  //用户账号
        img: '',   //用户的头像
        token: '', //token字符串
        loginIn: '' //是否登录
    },
    getters: {  //getter方法
        teacherId: state => {
            let teacherId = state.teacherId;
            if(!teacherId){
                //如果没有，从window中获取
                teacherId = JSON.parse(window.sessionStorage.getItem("teacherId"));
            }
            return teacherId;
        },
        teacherName: state => {
            let teacherName = state.teacherName;
            if(!teacherName){
                //如果没有，从window中获取
                teacherName = JSON.parse(window.sessionStorage.getItem("teacherName"));
            }
            return teacherName;
        },
        img: state => {
            let img = state.img;
            if(!img){
                //如果没有，从window中获取
                img = JSON.parse(window.sessionStorage.getItem("img"));
            }
            return img;
        },
        token: state => {
            let token = state.token;
            if(!token){
                //如果没有，从window中获取
                token = JSON.parse(window.sessionStorage.getItem("token"));
            }
            return token;
        },
      loginIn: state => {
          let loginIn = state.loginIn;
          if(!loginIn){
            //如果没有，从window中获取
            loginIn = JSON.parse(window.sessionStorage.getItem("loginIn"));
          }
          return loginIn;
        }
    },
    mutations: {    //setter方法
        setTeacherId:(state, teacherId) => {
            state.teacherId = teacherId;
            window.sessionStorage.setItem("teacherId", JSON.stringify(teacherId));
        },
        setTeacherName:(state, teacherName) => {
            state.teacherName = teacherName;
            window.sessionStorage.setItem("teacherName", JSON.stringify(teacherName));
        },
        setImg:(state, img) => {
            state.img = img;
            window.sessionStorage.setItem("img", JSON.stringify(img));
        },
        setToken:(state, token) => {
            state.token = token;
            window.sessionStorage.setItem("token", JSON.stringify(token));
        },
        setLoginIn:(state, loginIn) => {
          state.loginIn = loginIn;
          window.sessionStorage.setItem("loginIn", JSON.stringify(loginIn));
        }
    }
};
export default teacher;
